package com.pc.cloud.utils;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {

	public static String[] convert(String s) {

		String regex = "[`~!@#$^&*+()=|{}/':;',\\[\\].<>/?~！%#￥……&*（）—|{}【】‘；：”“'。，、?\\s-]";
		String [] ss = null;
		//		Pattern   p   =   Pattern.compile(regex);        
//        Matcher   m   =   p.matcher(s);
//		s = m.replaceAll(" ").trim();
		String regEx = "[\\u4e00-\\u9fa5]";
		String temp = s.replaceAll(regEx, "")+" ";
		Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(s);
		while (m.find()) {
			temp += (m.group()+" ");
		}
		ss = temp.split(regex);
		if(ss.length<=0){
			ss = new String[1];
			ss[0]="~";
		}
		return ss;
	}

	public static boolean checkNum(String s){
		String regx = "[A-Za-z0-9]|[A-Za-z0-9]+[A-Za-z0-9]";
		Pattern pattern = Pattern.compile(regx);
	    return pattern.matcher(s).matches();    
	}
	
	public static String[] trim(String[] s) {
		List<String> list = new ArrayList<String>();
		for (int i = 0; i < s.length; i++) {
			if (!s[i].equals("") && !s[i].equals(" ")) {
				list.add(s[i]);
			}
		}
		s = new String[list.size()];
		for (int i = 0; i < list.size(); i++) {
			s[i] = list.get(i);
		}
		return s;
	}
		
	public static String getPinYin(String zhongwen)
			throws BadHanyuPinyinOutputFormatCombination {

		String zhongWenPinYin = "";
		char[] chars = zhongwen.toCharArray();

		for (int i = 0; i < chars.length; i++) {
			String[] pinYin = PinyinHelper.toHanyuPinyinStringArray(chars[i],
					getDefaultOutputFormat());
			// 当转换不是中文字符时,返回null
			if (pinYin != null) {
				zhongWenPinYin += capitalize(pinYin[0]);
			} else {
				zhongWenPinYin += chars[i];
			}
		}
		;
		return StringUtils.delete(zhongWenPinYin.toLowerCase(), " ");
	}
	
	//支持多音字 如 空调   返回 kongkongdiaotiao
	public static String getPinYinEx(String zhongwen) throws BadHanyuPinyinOutputFormatCombination {
		String zhongWenPinYin = "";
		char[] chars = zhongwen.toCharArray();
		for (int i = 0; i < chars.length; i++) {
			String[] pinYin = PinyinHelper.toHanyuPinyinStringArray(chars[i],
					getDefaultOutputFormat());
			// 当转换不是中文字符时,返回null
			Map<String,Boolean> identical = new HashMap<String,Boolean>();
			if (pinYin != null) {
				for(String py : pinYin) {
					if(py != null) {
						Boolean exits = identical.get(py);
						if(exits == null) {
							zhongWenPinYin += py;
							identical.put(py, true);
						}
					}					
				}				
			} else {
				zhongWenPinYin += chars[i];
			}
		}			
		return zhongWenPinYin;
	}
	
	/**
	 * Default Format 默认输出格式
	 * 
	 * @return
	 */
	public static HanyuPinyinOutputFormat getDefaultOutputFormat() {
		HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
		format.setCaseType(HanyuPinyinCaseType.LOWERCASE);// 小写
		format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 没有音调数字
		format.setVCharType(HanyuPinyinVCharType.WITH_V);// u显示
		return format;
	}

	/**
	 * Capitalize 首字母大写
	 * 
	 * @param s
	 * @return
	 */
	public static String capitalize(String s) {
		char ch[];
		ch = s.toCharArray();
		if (ch[0] >= 'a' && ch[0] <= 'z') {
			ch[0] = (char) (ch[0] - 32);
		}
		String newString = new String(ch);
		return newString;
	}

}
